*********************REPLICATION FILE FOR*************************************
/*Strijbis, O. (forthcoming), "Deindustrialization fosters ethnonationalism;
a comparative analysis of ethnonational parties in Western Europe, 1918–2018",
European Political Science Review.*/
******************************************************************************

use ".../Strijbis_ethnonationalism_EPSR_data.dta", clear

*********************Interpolate data**************************
drop if year==.

sort id1
by id1: ipolate indu year, generate(i_indu)
by id1: ipolate foreigners year, generate(i_foreigners) epolate
by id1: ipolate postmat_high year, generate(i_postmat_high)
by id1: ipolate postmat_low year, generate(i_postmat_low)
by id1: ipolate postmat_med year, generate(i_postmat_med)

generate newvar=0
replace newvar=1 if elect_system=="Single"
rename newvar single_const
label variable single_const "Single electoral constituencies"

generate newvar=0
replace newvar=1 if elect_system=="Mixed"
rename newvar mixed_const
label variable mixed_const "Mixed constituencies"

generate newvar=0
replace newvar=(eg_rel*nat_pop)
rename newvar eg_abs

generate newvar=0
replace newvar=1 if party5!=.
rename newvar election

gen newvar=0
replace newvar=1 if id1=="SLOAUT"
replace newvar=2 if id1=="FLABEL"
replace newvar=3 if id1=="GERBEL"
replace newvar=4 if id1=="WALBEL"
replace newvar=5 if id1=="FARDEN"
replace newvar=6 if id1=="VAOITA"
replace newvar=7 if id1=="SWEFIN"
replace newvar=8 if id1=="BASFRA"
replace newvar=9 if id1=="CORFRA"
replace newvar=10 if id1=="SARITA"
replace newvar=11 if id1=="SLOITA"
replace newvar=12 if id1=="STYITA"
replace newvar=13 if id1=="FRINET"
replace newvar=14 if id1=="BASSPA"
replace newvar=15 if id1=="CATSPA"
replace newvar=16 if id1=="GALSPA"
replace newvar=17 if id1=="RAESWI"
replace newvar=18 if id1=="NIRUK"
replace newvar=19 if id1=="SCOUK"
replace newvar=20 if id1=="WELUK"
rename newvar panel

***generate dataset of means***

gen newvar=0
replace newvar=1 if electoral_period=="1918-1922"
replace newvar=2 if electoral_period=="1923-1927"
replace newvar=3 if electoral_period=="1928-1932"
replace newvar=4 if electoral_period=="1933-1939"
replace newvar=5 if electoral_period=="1945-1949"
replace newvar=6 if electoral_period=="1950-1954"
replace newvar=7 if electoral_period=="1955-1959"
replace newvar=8 if electoral_period=="1960-1964"
replace newvar=9 if electoral_period=="1965-1969"
replace newvar=10 if electoral_period=="1970-1974"
replace newvar=11 if electoral_period=="1975-1979"
replace newvar=12 if electoral_period=="1980-1984"
replace newvar=13 if electoral_period=="1985-1989"
replace newvar=14 if electoral_period=="1990-1994"
replace newvar=15 if electoral_period=="1995-1999"
replace newvar=16 if electoral_period=="2000-2004"
replace newvar=17 if electoral_period=="2005-2009"
replace newvar=18 if electoral_period=="2010-2014"
replace newvar=19 if electoral_period=="2015-2018"
rename newvar time

collapse (mean) time2 epv_abs epv_rel rel_epv repv_mean nat_pop eg_rel eg_abs indu federalism early_mob polityiv_l5 i_indu i_natpop const_size gdppc euintegration (median) indu_scale fragmented fragmented_country single_const mixed_const time (max) election, by(country ethnic_minority country_short ethnic_short id1 id2 electoral_period panel)

*drop cases without variance
drop if id1=="BASFRA"
drop if id1=="FRINET"
drop if id1=="RAESWI"
drop if id1=="SLOAUT"
*drop Faroese due to lack of data
drop if id1=="FARDEN"


****Analysis****

***FIGURE 1***
twoway (scatter repv_mean i_indu, yscale(range(0 1.5))) (lfit repv_mean i_indu, yscale(range(0 1.5)))
twoway (scatter repv_mean i_indu if repv_mean>0, yscale(range(0 1.5))) (lfit repv_mean i_indu if repv_mean>0, yscale(range(0 1.5)))
twoway (scatter repv_mean i_indu, yscale(range(0 1.5))) (lfit repv_mean i_indu if repv_mean>0, yscale(range(0 1.5))) (lfit repv_mean i_indu, yscale(range(0 1.5)))

***Reported correlations
pwcorr repv_mean i_indu indu_scale, obs sig
pwcorr repv_mean i_indu indu_scale if repv_mean>0, obs sig

***Recodings for multivariate analyses****
tsset panel time

*measure first - last riepm
by panel: egen lasttime = max(time2)
gen riepm_last=repv_mean if time2==lasttime
*no elections in Belgium in the last period => insert manually
replace riepm_last=0.004299909 if id1=="FLABEL"
replace riepm_last=0 if id1=="GERBEL"
replace riepm_last=0.053992506 if id1=="WALBEL"
by panel: egen riepm_last2=max(riepm_last)
by panel: egen firsttime = min(time2)
gen riepm_first=repv_mean if time2==firsttime
by panel: egen riepm_first2=max(riepm_first)
gen riepm_lastfirst=riepm_last2-riepm_first2
drop lasttime riepm_last riepm_first riepm_last2 riepm_first2

*Change eg_abs into mio-scale
replace eg_abs=(eg_abs/1000000)

*postwii variable
gen postwii=1
replace postwii=0 if electoral_period<"1945-1949"

*indu_scale with first time point =1 if >=4
bys postwii: tab2 indu_scale panel
gen indu_high=0
* >3
replace indu_high=1 if panel==2 | panel==3 | panel==4 | panel==6 | panel==15 | panel==19 | panel==20
* =3
replace indu_high=1 if panel==11 | panel==14 | panel==18
gen indu_low=0
replace indu_low=1 if indu_high==0

*fragmentation variables
replace fragmented_country=1 if id1=="FLABEL" & time2>13
replace fragmented_country=0 if id1=="NIRUK" & time2>6
gen fragmented_rev=-1*fragmented_country
gen fragmented2_rev=-1*fragmented

*Constituency size variable
replace const_size=1 if single_const==1

*label variables
label variable rel_epv "Ethnic Party Mobilisation"
label variable repv_mean "Relative IEPM"
label variable i_natpop "National pop. (abs)"
label variable eg_rel "Group size (rel)"
label variable eg_abs "Group size (abs)"
label variable i_indu "Share industry"
label variable mixed_const "Mixed system"
label variable single_const "MR system"
label variable polityiv_l5 "Democracy (t-1)"
label variable early_mob "Early mobilization"
label variable federalism "Federalism"
label variable rel_epv "RIEPM"
label variable repv_mean "RIEPM mean group size"
label variable indu_scale "Share industry"
label variable gdppc "National GDPPC"
label variable euintegration "EU integration"

***TABLE 2***
drop if repv_mean==.
drop if indu_scale==.
summarize rel_epv indu_scale i_indu eg_rel eg_abs polityiv_l5 single_const mixed_const federalism gdppc euintegration

***Table B1***
tab panel, gen(panel_)
tab time2, gen(time2_)

tab2 riepm_lastfirst ethnic_short if time2_1==1
bys ethnic_short: summarize rel_epv

*Calculate annual average growth rate:
sort panel time
*The zeros are a problem, I replace them with the minimal value of 0.01
gen rel_epv2=rel_epv
replace rel_epv2=0.01 if rel_epv==.
gen ag_riepm=(rel_epv2-l.rel_epv2)/l.rel_epv2
bys ethnic_short: egen aag_riepm=mean(ag_riepm)
bys ethnic_short: summarize rel_epv riepm_lastfirst aag_riepm
bys ethnic_short: corr rel_epv indu_scale

***FIGURE B1***
twoway (tsline rel_epv, by(id2) recast(connected) mlabsize(tiny) legend(size(small)) ttitle(""))  (tsline indu_scale, by(id2) recast(connected) mlabsize(tiny) legend(size(small)) ttitle("") yaxis(2))

***TABLE 3***
xtregar rel_epv indu_scale i.panel
est store BK1
xtregar rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.panel
est store BK2
xtregar rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const federalism euintegration gdppc i.panel
est store BK3
xtregar rel_epv i_indu eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.panel
est store BK4
esttab BK1 BK2 BK3 BK4, r2 label nomtitles nogaps se star(+ 0.1 * 0.05 ** 0.01 *** 0.001) stats(N N_g r2_w)

***Change in coefficient due to federalism or postwii?
bysort postwii: xtregar rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc federalism i.panel

***TABLE 4***
*Sample split: industrialization
bysort indu_high: xtregar rel_epv indu_scale i.panel
est store BK6_2
bysort indu_high: xtregar rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.panel
est store BK6
bysort indu_low: xtregar rel_epv indu_scale i.panel
est store BK7_2
bysort indu_low: xtregar rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.panel
est store BK7
esttab BK6_2 BK6 BK7_2 BK7, r2 label nomtitles nogaps se star(+ 0.1 * 0.05 ** 0.01 *** 0.001) stats(N N_g r2_w)

***TABLE 5***
*Sample split: fragmentation
bysort fragmented_country: xtregar rel_epv indu_scale i.panel
est store BK8
bysort fragmented_rev: xtregar rel_epv indu_scale i.panel
est store BK9
bysort fragmented_country: xtregar rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.panel
est store BK8_2
bysort fragmented_rev: xtregar rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.panel
est store BK9_2
esttab BK9 BK9_2 BK8 BK8_2, r2 label nomtitles nogaps se star(+ 0.1 * 0.05 ** 0.01 *** 0.001) stats(N N_g r2_w)

***TABLE 6***
*Analysis for mobilization variable assuming endogeneity of group size
xtregar repv_mean indu_scale i.panel
est store BK1
xtregar repv_mean indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.panel
est store BK2
xtregar repv_mean indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc federalism i.panel
est store BK3
xtregar repv_mean i_indu eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.panel
est store BK4
esttab BK1 BK2 BK3 BK4, r2 label nomtitles nogaps se star(+ 0.1 * 0.05 ** 0.01 *** 0.001) stats(N N_g r2_w)

*TABLE B2
xtreg rel_epv indu_scale i.time, i(panel) fe
est store DID1
xtreg rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.time, i(panel) fe
est store DID2
xtreg rel_epv indu_scale eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc federalism i.time, i(panel) fe
est store DID3
xtreg rel_epv i_indu eg_rel eg_abs polityiv_l5 single_const mixed_const euintegration gdppc i.time, i(panel) fe
est store DID4
esttab DID1 DID2 DID3 DID4, r2 label nomtitles nogaps se star(* 0.05 ** 0.01 *** 0.001) stats(N N_g r2_w)

*TABLE B3
*Construction of time lags
sort panel time
gen l_indu_scale=l1.indu_scale
gen l2_indu_scale=l2.indu_scale
gen l3_indu_scale=l3.indu_scale
gen l4_indu_scale=l4.indu_scale

xtregar rel_epv indu_scale  i.panel
est store BK1
xtregar rel_epv l_indu_scale  i.panel
est store BK2
xtregar rel_epv l2_indu_scale i.panel
est store BK3
xtregar rel_epv l3_indu_scale i.panel
est store BK4
xtregar rel_epv l4_indu_scale i.panel
est store BK5
esttab BK1 BK2 BK3 BK4 BK5, r2 label nomtitles nogaps se star(+ 0.1 * 0.05 ** 0.01 *** 0.001) stats(N N_g r2_w)


